<?php
namespace Macaca\Controller\Admin;


use Macaca\Module\Admin\User;
use Macaca\Module\Linker;
use Monkey\Controller;
use Monkey;

class Base extends Controller {

    /**
     * @var \Macaca\Module\Admin\User
     */
    public $moduleAdminUser;

    /**
     * @var \Macaca\Module\Linker
     */
    public $linker;

    /**
     * @param Monkey\App $app
     */
    public function __construct($app) {
        parent::__construct($app);
        $this->moduleAdminUser = new User($app);
        $this->linker = new Linker($app);
    }

    /**
     * action前置操作
     * @param $actionName
     */
    public function before($actionName) {
        parent::before($actionName);
        if ($actionName == 'login' or $actionName == 'loginView') {
            return;
        }
        if (!$this->moduleAdminUser->checkLogin()) { //  failed
            $jumpUrl = $this->linker->toFix('/admin/login');
            if ($this->request->isAjax()) {
                $this->returnJson(notice(false, $jumpUrl, 'Login failed validation'));
            }
            else {
                $this->redirect($jumpUrl, true);
            }
        }

    }

    public function writeLine($string) {
        $this->response->addBody($string . '<br/>');
    }

} 